package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teachar;

public class TeacharDao {
    private static Teachar[] teachars=new Teachar[5];

    public boolean addTeachar(Teachar t) {
        //找到null然后添加进去
        int index=-1;
        for (int i = 0; i < teachars.length; i++) {
            Teachar teachar=teachars[i];
            if(teachar==null){
                index =i;
                break;
            }
        }
        if (index == -1) {
            return false;
        }else {
            teachars[index]=t;
            return true;
        }
    }

    public Teachar[] findAllTeacher() {
        return teachars;
    }

    public void deleteTeacherById(String id) {
        //1.找到要删除id所对应的索引
        int index= getIndex(id);
        teachars[index]=null;

    }
    //得到索引方法
    public int getIndex(String id){
        //3.先假设索引不存在，如果找到就停止循环把，当前索引位置赋值给index
        int index=-1;

        //1.遍历数组得到全部元素找到对应元素索引位置
        for (int i = 0; i < teachars.length; i++) {
            //2把老师对象拿出来判断id是在那个索引
            Teachar teachar=teachars[i];
            if(teachar!=null&&teachar.getId().equals(id)){
                //找到了
                index=i;
                break;
            }

        }
        return index;
    }

    public void updateTeacher(String id, Teachar newTeacher) {
        //1.找到要修改的id索引
        int index=getIndex(id);

        //2.修改
        teachars[index]=newTeacher;
    }
}
